#include "client.h"
#include  <iostream>
using namespace std;


int num=0;
int i=0;
//unsigned int offset;

char m_buf[614400];

Client::Client(QWidget *parent)
    : QDialog(parent)
{

    setWindowTitle(tr("UDP Client")); //设置窗体的标题
    resize(800,800);
    //初始化各个控件
    Picture = new QLabel(this);
    TextLineEdit= new QLineEdit(tr("192.168.1.10"),this);
    ip = new QLabel(tr("目的ip"),this);
    ip->setFixedSize(300,20);
    name_you = new QLabel(tr("name"),this);
    name_you->setFixedSize(300,20);
    ReceiveTextEdit= new QTextEdit(this) ;
    ReceiveTextEdit->setFixedSize(400,30);
    CloseBtn = new QPushButton(tr ("START"),this) ;
    Picture->setFixedSize(640,480);
    Ee = new QPushButton(tr ("STOP"),this) ;

    //设置布局

    mainLayout=new QVBoxLayout(this) ;
    mainLayout->addWidget(Picture);
    mainLayout->addWidget(name_you);
    mainLayout->addWidget(ReceiveTextEdit);
    mainLayout->addWidget(ip);
    mainLayout->addWidget(TextLineEdit);
    mainLayout->addWidget(CloseBtn);

    mainLayout->addWidget(Ee);

    connect(TextLineEdit,SIGNAL(timeout()),this,SLOT(timeout()));

    connect (CloseBtn, SIGNAL (clicked()), this, SLOT (start()));//打开
    connect (Ee, SIGNAL (clicked()), this, SLOT (stop()));//关闭
    port =1234;    //设置UDP的端口号参数，指定在此端口上监听数据
    udpSocket = new QUdpSocket (this); //创建一个QudpSocket
    connect (udpSocket, SIGNAL(readyRead()), this, SLOT(dataReceived()));//当有数据到达IO设备时,触发dataReceived()函数

    bool result=udpSocket->bind(port);//绑定到指定的端口上
    if(!result)
    {
        QMessageBox::information(this, tr("error"), tr ("udp socket create error!"));
        return;
    }



}
//一旦有数据可读时,即通过readDatagram方法将数据读取并显示
void Client::dataReceived()
{
    QString name;


    char *recvBuf = new char[1288];
    memset(recvBuf, 0, 1288);
    while(udpSocket->hasPendingDatagrams())    //判断udpSocket是否有数据正在等待读取
    {

        QByteArray datagram;
        datagram.resize(udpSocket->pendingDatagramSize());//调整大小
        //udpSocket->readDatagram(datagram.data(), datagram.size());//读取第一个datagram的长度

        udpSocket->readDatagram(recvBuf, 1288);
        ImageFrameHead *mes = (ImageFrameHead *)recvBuf;

        if(mes->syncCode[0]==0xf0&&mes->syncCode[1]==0x5a&&mes->syncCode[2]==0xa5&&mes->syncCode[3]==0x0f){
            num=0;

           //delete[614400] m_buf;
        }

        mes->syncCode[5]==0xff?(name="张亚东"):(name="未知");
        //QString msg=datagram.data();

        memcpy(m_buf+1280*num,(recvBuf+(num==0?8:0)),(num==0?1288:1280));

        num++;
        i++;
        if(i==5){
        ReceiveTextEdit->insertPlainText(name);
        }
        //cout<<dec<<num<<endl;
        //cout<<num<<endl;
        if(num==480){
            QPixmap pixmap;
            QImage *img = new QImage((uchar*)m_buf,640,480,QImage::Format_RGB16);
            pixmap.convertFromImage(*img);
            Picture->setPixmap(pixmap);
            // ui->Picture->setNum(frame_count);
        }
    }
    // qDebug()<<recvBuf;
    //  ReceiveTextEdit-> insertPlainText(mes->syncCode[0]);    //显示数据内容

    delete[] recvBuf;
}

void Client::start()
{
     QString msg= TextLineEdit->text();
    udpSocket->writeDatagram("1",1,QHostAddress (msg),1234);
}

void Client::stop()
{
    QString msg= TextLineEdit->text();
    udpSocket->writeDatagram("0",1,QHostAddress(msg),1234);//192.168.1.10
}

Client::~Client()
{
}
